// 足球消息处理
export const footballMessage = ({ message, list, setList }) => {
  const { type, data } = message;

  const statusHandle = (d) => {
    // [赛事ID,赛事大状态,赛事小状态,赛事倒计时]
    const [eventId, bigState, smallState, gameTime] = d;
    const result = list.map((row) => {
      if (row.eventId === eventId) {
        return ({
          ...row,
          bigState,
          smallState,
          gameTime,
        });
      }

      return row;
    });
    setList([...result]);
  };
  const scoreHandle = (d) => {
    // [赛事ID,主队全场得分,客队全场得分,主队第1节得分,客队第1节得分,主队第2节得分
    // ,客队第2节得分,主队第3节得分,客队第3节得分,主队第4节得分,客队第4节得分,主队第加时得分,客队加时得分]
    const [
      eventId,
      homeTeamCurrentScore,
      awayTeamCurrentScore,
      homeTeamScoredInTheFirstQuarter,
      awayTeamScoredInTheFirstQuarter,
      homeTeamScoredTheSecondQuarter,
      awayTeamScoredTheSecondQuarter,
      homeTeamScoreInTheThirdQuarter,
      awayTeamScoredTheThirdQuarter,
      homeTeamScoredTheFourthQuarter,
      awayTeamScoredInTheFourthQuarter,
      homeTeamOvertimeScore,
      awayTeamOvertimeScore,
    ] = d;
    const result = list.map((row) => {
      if (row.eventId === eventId) {
        return ({
          ...row,
          score: {
            homeTeamCurrentScore,
            awayTeamCurrentScore,
            homeTeamScoredInTheFirstQuarter,
            awayTeamScoredInTheFirstQuarter,
            homeTeamScoredTheSecondQuarter,
            awayTeamScoredTheSecondQuarter,
            homeTeamScoreInTheThirdQuarter,
            awayTeamScoredTheThirdQuarter,
            homeTeamScoredTheFourthQuarter,
            awayTeamScoredInTheFourthQuarter,
            homeTeamOvertimeScore,
            awayTeamOvertimeScore,
          },
        });
      }

      return row;
    });
    setList([...result]);
  };

  if (type === 'status') return statusHandle(data);
  if (type === 'score') return scoreHandle(data);
};
// 篮球消息处理
export const basketballMessage = ({ message, list, setList }) => {
  const { type, data } = message;

  // 推送状态
  const statusHandle = (d) => {
    // [赛事ID,赛事大状态,赛事小状态,赛事扩展信息(可能为空)]
    const [eventId, bigState, smallState] = d || [];

    setList(list.map((row) => {
      if (row.eventId === eventId) {
        return {
          ...row,
          bigState,
          smallState,
        };
      }

      return row;
    }));
  };

  // 推送比分
  const scoreHandle = (d) => {
    // [赛事ID,主队全场得分,客队全场得分,主队半场得分,客队半场得分,进球时间,主队名称,客队名称]
    const [eventId, homeTeamCurrentScore, awayTeamCurrentScore, homeTeamHalfScore, awayTeamHalfScore, , homeTeamName, awayTeamName] = d || [];
    setList(list.map((row) => {
      if (row.eventId === eventId) {
        return {
          ...row,
          homeTeamName,
          awayTeamName,
          score: {
            ...row.score || {},
            homeTeamCurrentScore,
            awayTeamCurrentScore,
            homeTeamHalfScore,
            awayTeamHalfScore,
          },
        };
      }

      return row;
    }));
  };

  // 推送统计
  const staticHandle = (d) => {
    // [赛事ID,主队红牌数,客队红牌数,主队黄牌数,客队黄牌数,主队角球数,客队角球数,变化时间,主队名称,客队名称]
    const [eventId, homeTeamRedCard, awayRedCard, homeTeamYellowCard, awayTeamYellowCard, homeTeamCornerKick, awayTeamCornerKick, gameTime, homeTeamName, awayTeamName] = d || [];
    setList(list.map((row) => {
      if (row.eventId === eventId) {
        return {
          ...row,
          homeTeamName,
          awayTeamName,
          gameTime,
          score: {
            ...row.score || {},
            homeTeamRedCard,
            awayRedCard,
            homeTeamYellowCard,
            awayTeamYellowCard,
            homeTeamCornerKick,
            awayTeamCornerKick,
          },
        };
      }

      return row;
    }));
  };

  if (type === 'status') return statusHandle(data);
  if (type === 'score') return scoreHandle(data);
  if (type === 'static') return staticHandle(data);
};
